<?php
class Solution_LeePro extends WP_Widget{
	//khoi tao
	
	function Solution_LeePro(){
		parent::WP_Widget(
			'Solution_LeePro_Widget', 
			'06 - Travel Solution Conpany',
			array(
				'description'=>'Travel Solution Conpany'
			));
	}
	function widget( $args, $instance ) { 
		$get_leepro_option = new leeproOptions();
        extract($args); 
        $title = apply_filters( 'widget_title', 
            empty($instance['title']) ? '' : $instance['title'], 
            $instance, $this->id_base); 
        $text = apply_filters( 'widget_text', 
            $instance['text'], $instance ); 
		$option_show = apply_filters( 'widget_option_show', 
            $instance['option_show'], $instance ); 
        echo $before_widget; 
        if ( !empty( $title ) ) { 
            echo $before_title . $title . $after_title; }
			else
			{
				echo $before_title . 'Solution' . $after_title;
			}
			?> 
            <ul class="<?php if($option_show =='Only_Image' || $option_show =='Image_Title'){?>ul_img<?php }else {?>solution<?php } ?>"> 
            <?php $str = explode(",", $text); 
                foreach ($str as $s) :
						$get_solution_wg = get_post($s);
						if($get_solution_wg != null){
				?> 
						<li class="<?php if($option_show =='Only_Image' || $option_show =='Image_Title'){?>li_img<?php }?>">
								<a href="<?php echo $get_solution_wg->guid ;?>">
								<?php 
								if($option_show =='Only_Image' || $option_show =='Image_Title'){
									$tes_img = get_the_post_thumbnail($get_solution_wg->ID, 'thumbnail img_caption');
								if(!empty($tes_img)){
										echo get_the_post_thumbnail($get_solution_wg->ID, 'thumbnail img_solution');
								} else{;?>
								<img src="<?php bloginfo('template_url');?>/lib/img/thumber-1_jvlastnews_thumb.jpg"/></a>
								<?php }} 
								if($option_show =='Only_Title' || $option_show =='Image_Title'){
									echo '<a href="'.$get_solution_wg->guid.'"><span>'.$get_solution_wg->post_title.'</span></a>';
								}
								?>
								
						</li>
            <?php } endforeach; 
            ?> 
            </ul> 
			
        <?php 
        echo $after_widget; 
    }
	
	function update( $new_instance, $old_instance ) { 
		$instance = $old_instance; 
		$instance['title'] = strip_tags($new_instance['title']); 
		
		if ( current_user_can('unfiltered_html') ) {
			$instance['text'] =  $new_instance['text']; 
			$instance['option_show'] = $new_instance['option_show'];
			}
		else{
			$instance['option_show'] = stripslashes( 
				wp_filter_post_kses( addslashes($new_instance['option_show']) ) 
			); 
			$instance['text'] = stripslashes( 
				wp_filter_post_kses( addslashes($new_instance['text']) ) 
			); 
		}
		return $instance; 
	}
	function form( $instance ) { 
			$get_leepro_option_form = new leeproOptions();
			$instance = wp_parse_args( (array) $instance, 
				array( 'title' => '', 'text' => '','option_show'=>'' ) ); 
			$title = strip_tags($instance['title']); 
			$text = format_to_edit($instance['text']); 
			$option_show = format_to_edit($instance['option_show']); 
			$option_show_solution = array(
				'Only_Image' =>'Only_Image',
				'Only_Title' =>'Only_Title',
				'Image_Title' =>'Image_Title',
			);
	?> 
			<p> 
				<label for="<?php echo $this->get_field_id('title'); ?>"> 
					<?php _e('Title:'); ?> </label> 
				<input class="widefat" 
					id="<?php echo $this->get_field_id('title'); ?>" 
					name="<?php echo $this->get_field_name('title'); ?>" type="text"
					value="<?php echo  esc_attr($title);?>" /> 
				<label for="<?php echo $this->get_field_id('option_show'); ?>"><?php _e('Option show:'); ?></label>
				<select name="<?php echo $this->get_field_name('option_show')?>" id="<?php echo $this->get_field_id('option_show'); ?>">
					<?php foreach ( $option_show_solution as $set_show ) : ?>
							<option value="<?php echo $set_show ?>" <?php if($set_show==trim($option_show)){?> selected="selected" <?php }?>><?php echo $set_show ?></option>
					<?php endforeach; ?>
				</select>
			</p> 
				<label for="<?php echo $this->get_field_id('text'); ?>"> 
					<?php _e("Id post(Id space by ','):"); ?> </label> 
				<input class="widefat" 
					id="<?php echo $this->get_field_id('text'); ?>"
					name="<?php echo $this->get_field_name('text'); ?>" 
					value="<?php echo trim($text);?>" 	/> 
			<p> 
				<label for="<?php echo $this->get_field_id('text'); ?>"> 
					<?php _e('Example link:<br/>- http://youdomain.com/?p=119,<br/> -http://youdomain.com/?p=123<br/> - Input: 199,123'); ?> 
				</label> 
			</p> 
			<p>
				<ul>
				<?php $str = explode(",", $text); 
					$not_post = array();
                foreach ($str as $s) :
						$get_solution = get_post($s);
						if($get_solution!= null){
				?> 
						<li>p=<?php echo $s;?>&nbsp;<a target="_blank" href="<?php echo $get_solution->guid;?>"><?php echo $get_solution->post_title;?></a>&nbsp;|&nbsp;<a target="_blank" href="<?php echo get_option('home')?>/wp-admin/post.php?post=<?php echo $s;?>&action=edit">Edit</a></li>
				<?php }
					else{
						$not_post[]= $s;
					}
				endforeach; 
            ?> 
				</ul>
				<?php if($not_post !=null){?>
					<p>Warning: id not found:&nbsp;
						<?php 
							$total_post = count($not_post);
							$i = 1;
						foreach($not_post as $set_post){
								if($i < $total_post)
									echo $set_post.",";
								else
									echo $set_post;
							$i++;
						}?>
					</p>
				<?php }$not_post =null;?>
			</p>
	<?php 
		} 
} 
  
register_widget('Solution_LeePro'); 
